﻿// ===============================================================================
// DotNet.Platform 开发框架 2016 版权所有
// ===============================================================================

using System.Collections.Generic;
using System.Linq;
using DotNet.Core.Entity;
using DotNet.Collections;
using DotNet.Data.Extensions;
using DotNet.Extensions;
using DotNet.Utility;
using DotNet.Helper;

namespace DotNet.Core.Service
{
    /// <summary>
    /// 链接系统服务
    /// </summary>
    public class LinkSystemService
    {
        private static readonly Cache<string, LinkSystem> Cache = new Cache<string, LinkSystem>();
        private CoreRepository<LinkSystem> repos = new CoreRepository<LinkSystem>();

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="isCreate">是否新增</param>
        public BoolMessage Save(LinkSystem entity, bool isCreate)
        {
            var result = EntityHelper.ValidAsMessage(entity, "<br>");
            if (result.Failure)
            {
                return result;
            }

            var has = repos.Exists(p => p.Name == entity.Name && p.Id != entity.Id);
            if (has)
            {
                return new BoolMessage(false, "指定的名称已经存在");
            }

            if (isCreate)
            {
                repos.Insert(entity);
            }
            else
            {
                repos.Update(entity);
            }
            Cache.Set(entity.Name, entity);
            Cache.Set(entity.Id, entity);
            return BoolMessage.True;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids">主键数组</param>
        public BoolMessage Delete(string[] ids)
        {
            var names = repos.Query(p => p.Id.In(ids), p => p.Name).Select(p => p.Name).ToArray();
            repos.Delete(ids);
            Cache.Remove(names);
            Cache.Remove(ids);
            return BoolMessage.True;
        }

        /// <summary>
        /// 获取单个对象
        /// </summary>
        /// <param name="id">主键</param>
        public LinkSystem GetById(string id)
        {
            var entity = Cache.Get(id, () =>
            {
                return repos.Get(id);
            });
            return entity;
        }

        /// <summary>
        /// 获取单个对象
        /// </summary>
        /// <param name="name">名称</param>
        public LinkSystem GetByName(string name)
        {
            var entity = Cache.Get(name, () =>
            {
                return repos.Get(p => p.Name == name);
            });
            return entity;
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        public List<LinkSystem> GetList(string name)
        {
            var query = repos.SQL;
            if (name.IsNotEmpty())
            {
                name = name.Trim();
                query.Where(p => p.Name.Contains(name));
            }
            return repos.Query(query).ToList();
        }

        /// <summary>
        /// 获取名称列表
        /// </summary>
        public List<LinkSystem> GetCaptionList()
        {
            var query = repos.SQL;
            query.Select(p => p.Id, p => p.Name)
                .OrderByDesc(p => p.Name);
            return repos.Query(query).ToList();
        }

        /// <summary>
        /// 获取第一个名称
        /// </summary>
        public LinkSystem GetFirstCaption()
        {
            var query = repos.SQL;
            query.OrderByDesc(p => p.Name);
            return repos.Query(query).FirstOrDefault();
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="pageCondition">分页对象</param>
        /// <param name="name">名称关键字</param>
        public PageList<LinkSystem> GetPageList(PaginationCondition pageCondition, string name)
        {
            pageCondition.SetDefaultOrder(nameof(LinkSystem.Name));
            var query = repos.PageQuery(pageCondition);

            if (name.IsNotEmpty())
            {
                name = name.Trim();
                query.Where(p => p.Name.Contains(name));
            }
            query.ExcludeSelect(p => p.Contents);
            return repos.Page(query);
        }
    }
}
